Mapping Colors to Values with NetLogo Built-in Primitives

 

This document will show several ways to choose color in NetLogo

 

First, we will present the built-in mechanisms for handling color in NetLogo:

           - using color swatches widget

- using NetLogo colors: set pcolor color  or set color color

- using rgb colors: set pcolor [r g b] or set color [r g b]

- map numerical values to NetLogo colors: scale-color color value range1 range2

 

Then, we will introduce the palette NetLogo extension designed to complement the built-in color selection tools of NetLogo.

- using the scheme dialog to find an adequate scheme

- getting a ColorBrewer color: scheme-colors

- map value to color brewer schemes: scale-schemes

- map values color (multi-color) gradients: scale-gradient

 

Through out this how-to document we will set color on patches with using the pcolor patch variable using:

ask patches [set pcolor color-digit]

 

However this document is also applicable to turtles by using the appropriate commands:

ask turtles [set color color]

and using the doing the relevant customizations.

NetLogo Built-in Color Tools

Choosing a NetLogo Color

Color swatches

You can select it using the NetLogo Color Swatches. You open the Color Swatches Dialog, and copy the color value to the statement where you want to use it. Usually you will be using something like:

ask patches [set pcolor <copied-color>]

 

In the screenshot above the color choosen is lime – 2, and its set by using:

 ask patches [set pcolor lime - 2]

Color names

Another way to choose NetLogo colors is to simply use the NetLogo base color names:

 

ask patches set [pcolor red]

 

 

NetLogo's base colors names are:

gray red orange brown yellow green lime turquoise cyan sky blue violet magenta pink

 

Additionally, You can add a number from 0.1 to 5.0 to create a 50 red tints gradating from full red to white.

 

ask patches

        [set pcolor red + 2.5]

 

 

Or, you can subtract a number from 0.1 to 5.0 to a 50 red shades gradating from full red to black.

 

ask patches

        [set pcolor red – 2.5]

 

Color rgb

You can also use the raw rgb values for setting a color

 

ask patches

        [set pcolor [0 128 0]]

 

You should preview your rgb colors in a Color Chooser Dialog or you can find plenty of color picker in the web such as http://www.colr.org/.

Mapping a Value in Color

Numerical values can be mapped colors shades and tints seen in the NetLogo Color Swatches screenshot below:

 

Color-Scale Gradients

 

Using the scale-color primitive, the user can map numerical values to an entire row, or only the base color with its tints, or just the base color and its shades,.

 

This mapping can be called a sequential color mapping, since there is a clear perceptual ordering in the color gradation

Full scale color

to full-scale-color

ca

ask patches

  [

      let normalized-value  (pxcor + (pycor * world-width)) /

                               (world-width * world-height)

      let patch-value normalized-value  * 10 + 5 

      set plabel round patch-value

      ; change the color name to see another hue

      set pcolor scale-color (red - 5) patch-value  0 10 ]

end

 

Shade scale color

to shade-scale-color

ca

ask patches

  [

      let normalized-value  (pxcor + (pycor * world-width)) /

                            (world-width * world-height)

      let patch-value normalized-value  * 10 + 5 

      set plabel round patch-value

      ; note the change in the range2 of full-scale-color from 10 to 20

      set pcolor scale-color (red - 5) patch-value  0 20

   ]

end

Tint scale color

to tint-scale-color

ca

ask patches

  [

      let normalized-value  (pxcor + (pycor * world-width)) /

                            (world-width * world-height)

      let patch-value normalized-value  * 10 + 5 

      set plabel round patch-value

      ; note the change from the range1 of full-scale-color from 0 to -10

      set pcolor scale-color (red - 5) patch-value -10 10

   ]

end

RGB Gradients

Find your rgb color using an application color-chooser or a webpage color chooser http://www.colr.org/.

 

You can greate color gradients using netlogo built-in commands and rgb colors. Although creating these gradients is very simple, it is also a very limited way to produce Color Gradients.  You can only produce reliable gradients by using the saturated colors and increasing and decreasing their value.

 

RGB saturated colors:

 

red

[255  0   0 ]

blue

[ 0   0  255]

green

[ 0  255  0 ]

yellow

[255 255  0 ]

magenta

[255  0  255]

cyan

[0    0  255]

 

to simple-rgb-gradient

ca

ask patches

  [

      let normalized-value  (pxcor + (pycor * world-width)) /

                            (world-width * world-height)

      let patch-value normalized-value  * 255 + 128 

      set plabel round patch-value

      set pcolor   ( list  ( 255 - patch-value )  0 patch-value )

   ]

end

 

Mapping a Category to a Color

Another way of mapping colors is a categorical mapping where all the values take different values with no particular ordering.

 

 

ask n-of 50 patches [set plabel "Apple"]

ask n-of 50 patches [set plabel "Pear"]

ask n-of 50 patches [set plabel "Lime"]

ask patches

[

  if plabel = "Apple" [set pcolor red ]

  if plabel = "Pear" [set pcolor green ]

  if plabel = "Lime" [set pcolor lime ]

]